package com.chy.blog_server.modular.map.entity;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.chy.blog_server.core.util.UserUtil;
import com.chy.blog_server.modular.sharCore.BaseEntity;
import com.chy.blog_server.modular.system.entity.SysUser;
import lombok.Getter;
import lombok.Setter;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @description: 行政地区 实体类
 * @author: chenhuiyan
 * @create: 2025-03-22 15:16
 **/
@Getter
@Setter
@TableName(value = "map_district")
public class MapDistrict extends BaseEntity {

    /**
     * 名称
     */
    String name;

    /**
     * 省市县名称
     */
    @TableField(value = "long_name")
    String longName;

    /**
     * 编码
     */
    String adcode;

    /**
     * 上级编码
     */
    @TableField(value = "pa_adcode")
    String paAdcode;

    /**
     * 级别(province省/直辖市,city市,district区/县,street街道)
     */
    String level;

    /**
     * 位置
     */
    String center;

    /**
     * 添加数据时调用该方法生成
     * @param request
     */
    public void preInsert(HttpServletRequest request){
        SysUser user = UserUtil.getUserFromSession(request);
        if(ObjectUtil.isNotEmpty(user)){
            this.setCreateBy(user.getId());
            this.setUpdateBy(user.getId());
        }
        this.setId(IdUtil.simpleUUID());
        this.setCreateDate(new Date());
        this.setUpdateDate(new Date());
    }

    /**
     * 更新数据时调用该方法生成
     * @param request
     */
    public void preUpdate(HttpServletRequest request){
        SysUser user = UserUtil.getUserFromSession(request);
        if(ObjectUtil.isNotEmpty(user)){
            this.setUpdateBy(user.getId());
        }
        this.setUpdateDate(new Date());
    }
}
